package com.mobimagic.lockscreen.font;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.graphics.Typeface;

/**
 * Created by seekting on 2015/11/26.
 */
public class TypeFaceManager {

    private static TypeFaceManager typeFaceManager;

    //    android:fontFamily="sans-serif" // roboto regular
    //    android:fontFamily="sans-serif-light" // roboto light
    //    android:fontFamily="sans-serif-condensed" // roboto condensed
    //    android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
    public static final String FONT_ROBOTO_CONDENSED = "sans-serif-condensed";
    public static final String FONT_ROBOTO_ROBOTO_LIGHT = "sans-serif-light";
    public static final String FONT_ROBOTO_REGULAR = "sans-serif";
    public static final String FONT_ROBOTO_THIN = "sans-serif-thin";
    private Map<String, WeakReference<Typeface>> fonts = new HashMap<String, WeakReference<Typeface>>();

    private TypeFaceManager() {

    }

    public synchronized static TypeFaceManager getInstance() {
        if (typeFaceManager == null) {
            typeFaceManager = new TypeFaceManager();
        }
        return typeFaceManager;
    }

    public synchronized Typeface getTypeface(String name, int style) {
        WeakReference<Typeface> reference = fonts.get(getKey(name, style));
        Typeface result = null;
        if (reference != null) {
            result = reference.get();
        }
        if (result != null) {
            return result;
        } else {
            result = Typeface.create(name, style);
            fonts.put(name, new WeakReference<Typeface>(result));
            return result;
        }

    }

    private String getKey(String name, int style) {
        return name + style;
    }
}
